//
//  Main_Token_Model.m
//  ZITHER
//
//  Created by weixin on 2019/4/15.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Main_Token_Model.h"

@implementation Main_Token_Model

+ (instancetype)defaultManager {
    
    static Main_Token_Model *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[Main_Token_Model alloc] init];
        
    });
    return manager;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {}

- (NSString *)description {

    return [self yy_modelDescription];
}


- (instancetype)init {

    if (self = [super init]) {
        if ([self getUserInfoByLibrary]) {
            [self setUserInfoWithModel:[self getUserInfoByLibrary]];
        }
    }
    return self;
}

// 设置信息
- (void)setUserInfoWithModel:(Main_Token_Model *)model {
    /** token改变控制 */
    self.isToken = model.isToken;
    
    self.backgroundColor =  model.backgroundColor;//背景颜色
    
    self.wordColor333333 =  model.wordColor333333;//0x33333的文字颜色
    
    self.wordColor999999 =  model.wordColor999999;//0x99999的文字颜色
    
    self.postfixIcon =  model.postfixIcon;//图片后缀
    
    self.navTitleColor =  model.navTitleColor;//图片后缀
    self.umkey =  model.umkey; //2.友盟key
    self.wxKey =  model.wxKey; //2.微信分享key
    self.wxfxS =  model.wxfxS; //2.微信分享key
    self.txKey =  model.txKey;//3.腾讯分享key
    self.wbKey =  model.wbKey;//4.微博分享key
    self.wbfxS =  model.wbfxS;//4.微博分享key
    self.qzgx =  model.qzgx;//5.是否需要到
    self.pushKey =  model.pushKey;//图片后缀
    self.dia =  model.dia;//
    self.navTitleColor =  model.navTitleColor;//
    self.RootUrl =  model.RootUrl;//
    self.wu =  model.wu;//
    self.lj =  model.lj;//


    


    
    
    [self saveUserInfoToLibrary:model];

}

// 设置信息
- (void)setUserInfoWithNSDictionary:(NSDictionary *)div {

    /** token改变控制 */
    self.isToken = div[@"isToken"];
    
    self.backgroundColor = div[@"backgroundColor"];//背景颜色
    
    self.wordColor333333 = div[@"wordColor333333"];//0x33333的文字颜色
    
    self.wordColor999999 = div[@"wordColor999999"];//0x99999的文字颜色
    
    self.postfixIcon = div[@"postfixIcon"];//图片后缀
    
    self.navTitleColor =  div[@"navTitleColor"];//图片后缀
    self.umkey =  div[@"umkey"]; //2.友盟key
    self.wxKey =   div[@"wxKey"]; //2.微信分享key
    self.wxfxS =   div[@"wxfxS"]; //2.微信分享key
    self.txKey =   div[@"txKey"];//3.腾讯分享key
    self.wbKey =   div[@"wbKey"];//4.微博分享key
    self.wbfxS =   div[@"wbfxS"];//4.微博分享key
    self.qzgx =   div[@"qzgx"];//5.是否需要到
    self.pushKey =   div[@"pushKey"];//图片后缀
    self.dia =   div[@"dia"];//图片后缀
    self.navTitleColor =   div[@"navTitleColor"];//图片后缀
    self.RootUrl =   div[@"RootUrl"];//图片后缀
    self.wu =   div[@"wu"];//图片后缀
    self.lj =  div[@"lj"];//

    
}


-(void)func2:(NSDictionary*)dic{
    if (dic == nil) {
        Main_Token_Model_MANAGER.isToken = @"1";
        [Main_Token_Model_MANAGER saveUserInfoSelf];
        return;
    }
    
    //1.友盟key
    Main_Token_Model_MANAGER.umkey =dic[@"ymk"];
    //2.微信分享key
    Main_Token_Model_MANAGER.wxKey = dic[@"wxfxk"];
    Main_Token_Model_MANAGER.wxfxS = dic[@"wxfxS"];
    //3.腾讯分享key
    Main_Token_Model_MANAGER.txKey = dic[@"txfxk"];
    //4.微博分享key
    Main_Token_Model_MANAGER.wbKey = dic[@"wbfxk"];
    Main_Token_Model_MANAGER.wbfxS = dic[@"wbfxS"];
    
    //5.
    Main_Token_Model_MANAGER.qzgx = dic[@"qzgx"];
    //6.推送key
    Main_Token_Model_MANAGER.pushKey = dic[@"tsk"];
    //5.是否需要到
    Main_Token_Model_MANAGER.dia = dic[@"dia"];
    //    NSString * str = [NSString stringWithFormat:@"%@/v1-1",dic[@"u"]];
    
    
    Main_Token_Model_MANAGER.wu = dic[@"wu"];
    Main_Token_Model_MANAGER.lj = [NSString stringWithFormat:@"/%@", dic[@"lj"]];
    Main_Token_Model_MANAGER.isToken = dic[@"t"];
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"0"]) {
        
        if (![Main_Token_Model_MANAGER.RootUrl isEqualToString:dic[@"u"]]) {
            if (URL_SERVICE ==4) {
                Main_Token_Model_MANAGER.RootUrl = dic[@"u"];
            }
            [[AFHTTPSessionManager shareManager] jwxSecurity];
        }
    }
    //    else{
    //        Main_Token_Model_MANAGER.RootUrl = RootUrl;
    //    }
    //    Main_Token_Model_MANAGER.RootUrl = RootUrl;
    
    [Main_Token_Model_MANAGER saveUserInfoSelf];
    
}



// 保存到沙盒
- (void)saveUserInfoToLibrary:(Main_Token_Model *)model {
    NSData *modelData = [NSKeyedArchiver archivedDataWithRootObject:model];
    [kUSER_DEFAULTS setObject:modelData forKey:@"Main_Token_Model"];
    [kUSER_DEFAULTS synchronize];
}

// 从沙盒获取用户信息
- (Main_Token_Model *)getUserInfoByLibrary {
    NSData *modelData = [kUSER_DEFAULTS objectForKey:@"Main_Token_Model"];
    [kUSER_DEFAULTS synchronize];
    Main_Token_Model *userInfo = [NSKeyedUnarchiver unarchiveObjectWithData:modelData];
    return userInfo;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [self yy_modelEncodeWithCoder:aCoder];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    return [self yy_modelInitWithCoder:aDecoder];
}
- (void)saveUserInfoSelf{
    [self saveUserInfoToLibrary:self];
}
@end
